﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;


public class BundleConfig {

    private static BundleConfig instance;

    private Dictionary<string, string> folderDic;

    public static BundleConfig Instance()
    {
        if (instance == null)
        {
            instance = new BundleConfig();
        }
        return instance;
    }

    private BundleConfig()
    {

    }

    private BundleConfigData bundleConfigData;

    public void Init()
    {
        folderDic = new Dictionary<string, string>();
        Process(ResourcePath.BundleConfigPath);
    }

    private void Process(string path)
    {
        if (!string.IsNullOrEmpty(path)) {
            using (StreamReader streamReader = new StreamReader(path))
            {
                string content = streamReader.ReadToEnd();
                bundleConfigData = JsonConvert.DeserializeObject<BundleConfigData>(content);

                if (bundleConfigData != null)
                {
                    for (int i = 0; i < bundleConfigData.folderBundle.Count; ++i)
                    {
                        // for future fast search
                        folderDic.Add(bundleConfigData.folderBundle[i].foldName, bundleConfigData.folderBundle[i].foldName);
                    }
                }
            }
        }
    }

    public bool ContainsInFolder(string file, out string folderName)
    {
        bool result = false;
        folderName = string.Empty;
        string[] splitInfos = file.Split('/');
        if (splitInfos.Length != 0 && folderDic.ContainsKey(splitInfos[0]))
        {
            result = true;
            folderName = splitInfos[0];
        }
        return result;
    }

    private class BundleConfigData
    {
        public List<FolderPair> folderBundle;
        public List<FolderPair> exmaple;
    }

    private class FolderPair
    {
        public string foldName;
    }
}
